# registering the spreadsheet

cville_sheets <- gs_url("https://docs.google.com/spreadsheets/d/1ISo4boBzGck4N8Jje_kDKCM8eVP6ftLCbXGbnHCx-f8/edit#gid=0")
## Sheet-identifying info appears to be a browser URL.
## googlesheets will attempt to extract sheet key from the URL.
## Putative key: 1ISo4boBzGck4N8Jje_kDKCM8eVP6ftLCbXGbnHCx-f8
## Worksheets feed constructed with public visibility
cville_tracts <- tracts(state = "VA", county = c("Albemarle", "Fluvanna", "Greene", "Nelson", "Charlottesville city"))

cville_race <- cville_sheets %>%
  gs_read(ws= "geo_race")
## Accessing worksheet titled 'geo_race'.
## Parsed with column specification:
## cols(
##   Id = col_character(),
##   Id2 = col_double(),
##   Geography = col_character(),
##   N_total = col_integer(),
##   P_total = col_integer(),
##   N_white = col_integer(),
##   P_white = col_double(),
##   N_black = col_integer(),
##   P_black = col_double(),
##   N_AmIn = col_integer(),
##   P_AmIn = col_double(),
##   N_asian = col_integer(),
##   P_asian = col_double(),
##   N_hawaiian = col_integer(),
##   P_hawaiian = col_double(),
##   N_other = col_integer(),
##   P_other = col_double(),
##   N_hispanic = col_integer(),
##   P_hispanic = col_double()
## )
cville_race_geo <- geo_join(cville_tracts, cville_race, by_sp = "GEOID", by_df = "Id2", how = "inner")
pal <- colorBin("PuBu", cville_race_geo$P_white, 6)

leaflet(cville_race_geo) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor = ~pal(P_white),
    fillOpacity = 0.4,
    color = "Black",
    weight = 0.7
  ) %>%
  addLegend(
    position = "bottomright",
    pal = pal,
    values = ~P_white,
    labFormat = labelFormat(suffix = "%"),
    title = "Percent White"
  ) %>%
  addMiniMap(
    tiles = providers$CartoDB.Positron,
    position="topright",
    toggleDisplay = TRUE
  )
make_map <- function(variable_name, race) {
  pal <- colorBin("PuBu", variable_name, 6)

  m <- leaflet(cville_race_geo) %>%
    addProviderTiles(providers$CartoDB.Positron) %>%
    addPolygons(
      fillColor = ~pal(variable_name),
      fillOpacity = 0.4,
      color = "Black",
      weight = 0.7
    ) %>%
    addLegend(
      position = "bottomright",
      pal = pal,
      values = ~P_white,
      labFormat = labelFormat(suffix = "%"),
      title = paste0("Percent ", race)
    ) %>%
    addMiniMap(
      tiles = providers$CartoDB.Positron,
      position="topright",
      toggleDisplay = TRUE
    )
  return(m)
}
white <- make_map(variable_name = cville_race_geo$P_white, race = "White")

white
black <- make_map(variable_name = cville_race_geo$P_black, race = "Black")

black
hispanic <- make_map(variable_name = cville_race_geo$P_hispanic, race = "Hispanic")

hispanic
asian <- make_map(variable_name = cville_race_geo$P_asian, race = "Asian")

asian
cville_dissimilarity <- cville_sheets %>%
  gs_read(ws = 2)
## Accessing worksheet titled 'dissimilarity_index'.
## Parsed with column specification:
## cols(
##   Race = col_character(),
##   Dissimilarity_index = col_double(),
##   Proportion = col_double(),
##   Total = col_integer()
## )
cville_dissimilarity$Race <- factor(cville_dissimilarity$Race, levels = c("White", "Black", "Hispanic", "Asian", "Other", "American Indian/Alaska Native", "Hawaiian/Pacific Islander"))

ggplot(cville_dissimilarity, aes(x = Race, y = Proportion)) +
  geom_bar(stat = "identity", fill = "#f27059") + theme_hc() +
  theme(axis.text.x = element_text(angle = 70, hjust = 1)) + 
  ggtitle("Racial/Ethnic Representation")

cville_dissimilarity$Race <- factor(cville_dissimilarity$Race, levels = c("Asian", "Other", "Black", "White", "Hawaiian/Pacific Islander", "Hispanic", "American Indian/Alaska Native"))

ggplot(cville_dissimilarity, aes(x = Race, y = Dissimilarity_index)) +
  geom_bar(stat = "identity", fill = "#f27059") + theme_hc() +
  theme(axis.text.x = element_text(angle = 70, hjust = 1)) + 
  ggtitle("Dissimilarity Indices")